/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.EnumProperty;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bus.entity.VaccineManage;
import com.thinkgem.jeesite.modules.bus.service.VaccineManageService;

import java.util.List;
import java.util.Map;

/**
 * 疫苗维护表Controller
 * @author sy
 * @version 2019-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/bus/vaccineManage")
public class VaccineManageController extends BaseController {

	@Autowired
	private VaccineManageService vaccineManageService;
	@Autowired
	private DictService dictService;
	
	@ModelAttribute
	public VaccineManage get(@RequestParam(required=false) String id) {
		VaccineManage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = vaccineManageService.get(id);
		}
		if (entity == null){
			entity = new VaccineManage();
		}
		return entity;
	}
	
	@RequiresPermissions("bus:vaccineManage:view")
	@RequestMapping(value = {"list", ""})
	public String list(VaccineManage vaccineManage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VaccineManage> page = vaccineManageService.findPage(new Page<VaccineManage>(request, response), vaccineManage); 
		model.addAttribute("page", page);
		return "modules/bus/vaccineManageList";
	}

	@RequiresPermissions("bus:vaccineManage:view")
	@RequestMapping(value = "form")
	public String form(VaccineManage vaccineManage, Model model) {
		Dict dict=new Dict();
		dict.setType("animalType");
		List<Dict> list= DictUtils.getDictList("animalType");
		if(StringUtils.isNotEmpty(vaccineManage.getId())){
			dict.setValue(vaccineManage.getAnimalTypeCode());
			List<Dict> checkList=dictService.findLabelByValue(dict);
			vaccineManage.setDictList(checkList);
		}
		model.addAttribute("list",list);
		model.addAttribute("vaccineManage", vaccineManage);
		return "modules/bus/vaccineManageForm";
	}
    /**
     *
     * @Title: save
     * @author: mengying
     * @date: 2019年06月17日 14:03
     * @Description: 疫苗维护新增、编辑疫苗保存
     * @param vaccineManage
     * @return: String
     */
	@RequiresPermissions("bus:vaccineManage:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public String save(VaccineManage vaccineManage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, vaccineManage)){
			return form(vaccineManage, model);
		}
		if(StringUtils.isNotEmpty(vaccineManage.getId())){
           String name= vaccineManageService.validateEditName(vaccineManage.getId());
           if(name.equals(vaccineManage.getName())){
               vaccineManageService.save(vaccineManage);
               addMessage(redirectAttributes, "保存疫苗成功");
			   return EnumProperty.SaveEnum.SAVESUCCESS.getKey();
           }
        }
        Integer count= vaccineManageService.validateName(vaccineManage.getName());
        if(count>0){
            addMessage(redirectAttributes, "疫苗名称重复,保存失败！");
			return EnumProperty.SaveEnum.SAVEFAILURE.getKey();
        }else{
            vaccineManageService.save(vaccineManage);
            addMessage(redirectAttributes, "保存疫苗成功");
			return EnumProperty.SaveEnum.SAVESUCCESS.getKey();
        }
    //    return "redirect:"+Global.getAdminPath()+"/bus/vaccineManage/?repage";
	}
	
	@RequiresPermissions("bus:vaccineManage:edit")
	@RequestMapping(value = "delete")
	public String delete(VaccineManage vaccineManage, RedirectAttributes redirectAttributes) {
		vaccineManageService.delete(vaccineManage);
		addMessage(redirectAttributes, "删除疫苗成功");
		return "redirect:"+Global.getAdminPath()+"/bus/vaccineManage/findVaccineList/?repage";
	}

	/**
	 *
	 * @Title: findVaccineTypeListByAnimalTypeCode
	 * @author: sy
	 * @date: 2019年06月14日 16:03
	 * @Description: 根据动物种类查询疫苗种类
	 * @param vaccineManage
	 * @return: java.util.List<java.util.Map<java.lang.String,java.lang.String>>
	 */
	@ResponseBody
	@RequestMapping(value = "findVaccineTypeListByAnimalTypeCode")
	public List<Map<String,String>> findVaccineTypeListByAnimalTypeCode(VaccineManage vaccineManage) {
		List<Map<String,String>> list = vaccineManageService.findVaccineTypeListByAnimalTypeCode(vaccineManage);
		return list;
	}

	/**
	 * 
	 * @Title: findListByVaccineTypeCode
	 * @author: sy
	 * @date: 2019年06月14日 16:49
	 * @Description: 根据疫苗种类查询疫苗
	 * @param vaccineManage
	 * @return: java.util.List<java.util.Map<java.lang.String,java.lang.String>>
	 */
	@ResponseBody
	@RequestMapping(value = "findListByVaccineTypeCode")
	public List<Map<String,String>> findListByVaccineTypeCode(VaccineManage vaccineManage) {
		List<Map<String,String>> list = vaccineManageService.findListByVaccineTypeCode(vaccineManage);
		return list;
	}

	@RequiresPermissions("bus:vaccineManage:view")
	@RequestMapping(value = {"vaccineManageList"})
	public String vaccineManageList(VaccineManage vaccineManage, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Page<VaccineManage> page = vaccineManageService.findPage(new Page<VaccineManage>(request, response), vaccineManage);
//		model.addAttribute("page", page);
		return "modules/bus/vaccineManageList";
	}
	/*
	 * @MethodName: findVaccineList
	 * @Description: 疫苗维护列表
	 * @Param: [vaccineManage,request,response,model]
	 * @Return: String
	 * @Author: my
	 * @Date: 2019/7/23
	 **/
	@RequiresPermissions("bus:vaccineManage:view")
	@RequestMapping(value = "findVaccineList")
	public String findVaccineList(VaccineManage vaccineManage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VaccineManage> page = vaccineManageService.findVaccineList(new Page<VaccineManage>(request, response), vaccineManage);
		model.addAttribute("page", page);
		return "modules/bus/vaccineManageList";
	}

}